﻿using System.Web;
using System.Web.SessionState;
using System.Web.Security;
using System.Configuration;
using System.Web.Mvc;


namespace Pe.Petecos.Comercial.Presentacion.Core.Security
{
    public sealed class HttpSessionContext
    {
        public static string USER_APPLICATION = "SessionUserPetecosContext";
        /// <summary>
        /// Obtiene la sesión actual.
        /// </summary>
        /// <returns></returns>
        public static CuentaUsuario CurrentAccount()
        {
            var result = HttpContext.Current.Session != null ? (CuentaUsuario)HttpContext.Current.Session[USER_APPLICATION] : null;

            return result;
        }
        /// <summary>
        /// Obtiene la sesión actual.
        /// </summary>
        /// <returns></returns>
        public static void SetAccount(CuentaUsuario account)
        {
            HttpContext.Current.Session.Add(USER_APPLICATION, account);
        }

        public static void RemoveAccount() {
            FormsAuthentication.SignOut();
            HttpContext.Current.Session.Abandon();
            HttpContext.Current.Session.Clear();
        }

        public static RedirectResult LogOut()
        {
            FormsAuthentication.SignOut();
            HttpContext.Current.Session.Abandon();
            HttpContext.Current.Session.Clear();

            string LogoutUrl = string.Format("{0}", System.Web.Security.FormsAuthentication.LoginUrl);

            return new RedirectResult(LogoutUrl);
        }
    }
}
